<?php
/**
 * Created by PhpStorm.
 * User: Дима
 * Date: 18.01.14
 * Time: 15:16
 */

namespace Models;

class ModelBase extends \Phalcon\Mvc\Model
{
    private static $__models=array();

    public static function findByPk($pk)
    {
        if($pk && is_integer($pk)) return parent::findFirst($pk);
        else return false;
    }

    public static function findAll($params=array())
    {
        $result=array();

        if($models=parent::find($params)){
            foreach($models as $model){
                if(isset($model->id)) $result[$model->id]=$model;
                else $result[]=$model;
            }
        }

        return $result;
    }

    public static function model($class=__CLASS__)
    {
        if(!isset(self::$__models[$class])) self::$__models[$class]=new $class();
        return self::$__models[$class];
    }

}